﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Net.Mail;
using System.IO;
using System.Configuration;
using ECCIT.Security;
using OgilvyOne.UKNow.Globalization;

public partial class cn_FinderPwd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["emailT"]!=null)
            {
                string emi = Request.Form["emailT"].ToString();
                string isok = PwdBud(emi);
                if (isok != null)
                {
                  bool  issend=  sendEmail(emi, isok);
                  Response.Write(issend.ToString());

                }

             

            }
            
        }
    }
    private bool sendEmail(string fromAs,string pwd)
    {

       return  sendMail(fromAs,pwd);

        
    }
    private   string  PwdBud(string emil)
    {

       
        MemberBLL mbb = new MemberBLL((int)LanguageType.cn);
        IList<MemberModel> lmm = mbb.CheckEmail(emil);
        if (lmm.Count>0)
        {

            lmm[0].Password = emil.Replace("@", "x").Replace(".", "_");
            string restr = lmm[0].Password;
            lmm[0].Password = MD5Crypto.GetMD5Hash(lmm[0].Password);
            if (mbb.Save(lmm[0]).Result)
            {
                return restr;
            }
            else
            {
                return null;
            }
                
          

        }
        else
        {
            return null;

        }

       
    }
    public static bool sendMail(string toMail,string pwd)
    {
        StreamReader sr = null;
        try
        {

            MailMessage dim = new MailMessage();
            //new MailAddress("发件人邮箱", "发件人名称");
            dim.From = new MailAddress(ConfigurationManager.AppSettings["FromAddres"], ConfigurationManager.AppSettings["EmilName"] + pwd);
            dim.To.Add(toMail);//收件人
            dim.Subject = ConfigurationManager.AppSettings["EmilTitle"];//标题
            dim.Body = ConfigurationManager.AppSettings["EmilCont"] + pwd;
            dim.IsBodyHtml = true;
            dim.BodyEncoding = System.Text.Encoding.UTF8;
            dim.Priority = System.Net.Mail.MailPriority.Normal;
            SmtpClient smtpClient = new SmtpClient();
            //发件人密码
            smtpClient.Credentials = new System.Net.NetworkCredential(dim.From.Address, ConfigurationManager.AppSettings["smtpSendPwd"]);
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //邮件发送服务器
            smtpClient.Host = ConfigurationManager.AppSettings["smtpClientHost"];
           
            smtpClient.Send(dim);
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            if (sr != null)
            {
                sr.Close();
                sr.Dispose();
            }
        }
        return true;
    }
    public static string generateEmail(string userName, string code, string param1, string param2, string templateName)
    {
        StreamReader sr = new StreamReader(getPath(string.Format("{0}.html", templateName)));
        string str = null;
        try
        {
            str = sr.ReadToEnd();
            str = str.Replace("$userName$", userName);
            str = str.Replace("$code$", code);
            str = str.Replace("$param1$", param1);
            str = str.Replace("$param2$", param2);
        }
        catch (Exception e)
        {
            e.ToString();
        }
        finally
        {
            sr.Close();
        }
        return str;
    }
    private static string getPath(string path)
    {
        return HttpContext.Current.Server.MapPath(path);
    }
}